package com.lufei4.chestnut.view;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.lufei4.chestnut.R;
import com.lufei4.chestnut.bean.NewsTypeColumnListBean;
import com.lufei4.chestnut.bean.NewsTypeMobileBean;
import com.lufei4.chestnut.bean.NewsTypeTitleBean;
import com.lufei4.chestnut.network.BaseActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * @ProjectName: Chestnut
 * @Package: com.lufei4.chestnut.view
 * @ClassName: MainActivity3
 * @Description: java类作用描述
 * @Author: liushijiang 15833013864@163.com
 * @CreateDate: 2020/12/29 17:54
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/12/29 17:54
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */

public class MainActivity3 extends BaseActivity {
    @BindView(R.id.rl_top_bar)
    RelativeLayout rlTopBar;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.publish_title)
    TextView publishTitle;
    @BindView(R.id.relative_title)
    RelativeLayout relativeTitle;
    @BindView(R.id.view_2)
    View view2;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.btn1)
    Button btn1;
    private NewsSettingAdapter2 mAdapter;


    @Override
    public int getLayoutId() {
        return R.layout.layout_main3;
    }

    @Override
    public void initView() {

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new NewsSettingAdapter2(new ArrayList<>());
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter);
        makeDataSet();

        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                 mSuspensionHeight = btn1.getHeight();
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                View view = recyclerView.getLayoutManager().findViewByPosition(mCurrentPosition + 1);
                if (view != null) {
                    if (view.getTop() <= mSuspensionHeight) {
                        btn1.setY(-(mSuspensionHeight - view.getTop()));
                    } else {
                        btn1.setY(0);
                    }
                }
            }
        });
    }
    int mSuspensionHeight,mCurrentPosition=0;
    private String initData2() {
        StringBuilder sb = new StringBuilder();
        try {
            InputStream inputStream = getResources().getAssets().open("sshy.json");
            InputStreamReader isr = new InputStreamReader(inputStream);
            BufferedReader reader = new BufferedReader(isr);
            String jsonLine;
            while ((jsonLine = reader.readLine()) != null) {
                sb.append(jsonLine);
            }
            reader.close();
            isr.close();
            inputStream.close();

            String result = sb.toString().replaceAll(" ", "");
            Log.d("hhhhhhhhh", "result: " + result);
            return result;
        } catch (Exception e) {
            Log.d("hhhhhhhhh", "e: " + e.getMessage());
            e.printStackTrace();
            return "";
        }
    }

    private void makeDataSet() {
        NewsTypeColumnListBean newsTypeColumnListBean = new Gson().fromJson(initData2(), NewsTypeColumnListBean.class);
        mAdapter.addData(new NewsTypeTitleBean("接收消息的账号"));
        mAdapter.addData(new NewsTypeMobileBean("121221"));
        mAdapter.addData(new NewsTypeMobileBean("121212"));
        mAdapter.addData(new NewsTypeMobileBean("121221"));
        mAdapter.addData(new NewsTypeMobileBean("121212"));
        mAdapter.addData(new NewsTypeMobileBean("121221"));
        mAdapter.addData(new NewsTypeMobileBean("121212"));
        mAdapter.addData(new NewsTypeMobileBean("121221"));
        mAdapter.addData(new NewsTypeMobileBean("121212"));
        mAdapter.addData(new NewsTypeTitleBean("消息免打扰设置"));
        mAdapter.addData(new NewsTypeMobileBean("121221"));
        mAdapter.addData(new NewsTypeMobileBean("121212"));
        mAdapter.addData(new NewsTypeMobileBean("121221"));
        mAdapter.addData(new NewsTypeMobileBean("121212"));
        mAdapter.addData(new NewsTypeMobileBean("121221"));
        mAdapter.addData(new NewsTypeMobileBean("121212"));
        mAdapter.addData(new NewsTypeMobileBean("121221"));
        mAdapter.addData(new NewsTypeMobileBean("121212"));
        mAdapter.addData(newsTypeColumnListBean.data);
    }

    @OnClick({R.id.iv_back, R.id.publish_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                ToastUtils.showShort("00000");

                break;
            case R.id.publish_title:
                break;
        }
    }

    private void initData() {
        OkGo.<String>post("https://dev-tninfo.tpyzq.com/message/getSetColumnList")
                .params("context", "E129723FDBFE545280E41E6CE1E696F333A9B07CB1ABEC3CF60DF15EF7261C8DB2C856F3B64221F124FFA6E0CFC5AAFA4E67D9DC2054B82590ACEC7A1E99C6CEC6D0FF4BC65CD8342E5503D31DE748CE")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ToastUtils.showShort("onSuccess");
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.showShort("error");
                    }

                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
